﻿//-----------------------------------------------------------------------
// <copyright file="StringExtensions.cs" company="Microsoft Corporation">
//     Copyright Microsoft Corporation. All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.) This is sample code only, do not use in production environments.
// </copyright>
//-----------------------------------------------------------------------
namespace Microsoft.ALMRangers.VsarTreasureMap.WindowsStoreApp.Extensions
{
    using System;
    using System.Globalization;

    /// <summary>
    /// Extensions for the string class
    /// </summary>
    public static class StringExtensions
    {
        /// <summary>
        /// Truncates at word.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <param name="length">The length.</param>
        /// <returns>A shorter string.</returns>
        public static string TruncateAtWord(this string input, int length)
        {
            if (input == null || input.Length < length)
            {
                return input;
            }

            var nextSpace = input.LastIndexOf(" ", length, StringComparison.CurrentCulture);
            return string.Format(CultureInfo.InvariantCulture, "{0}...", input.Substring(0, (nextSpace > 0) ? nextSpace : length).Trim());
        }
    }
}
